package com.education.welco.service.impl;

import com.education.common.utils.Convert;
import com.education.welco.mapper.MineResCatalogMapper;
import com.education.welco.mapper.MineResTableMapper;
import com.education.welco.pojo.MineResCatalog;
import com.education.welco.pojo.MineResTable;
import com.education.welco.pojo.vo.MineResCatalogVO;
import com.education.welco.service.IMineResCatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 我的资源库目录 服务层实现
 *
 * @Author: liftsail
 * @Date: 2022-10-17
 * @Description: 不积跬步无以至千里
 */
@Service
public class MineResCatalogServiceImpl implements IMineResCatalogService {

    @Autowired
    private MineResCatalogMapper mineResCatalogMapper;

    @Autowired
    private MineResTableMapper mineResTableMapper;

    /**
     * 查询我的资源库目录信息
     *
     * @param catalogId 我的资源库目录ID
     * @return 我的资源库目录信息
     */
    @Override
    public MineResCatalog selectMineResCatalogById(Integer catalogId) {
        return mineResCatalogMapper.selectMineResCatalogById(catalogId);
    }

    /**
     * 查询我的资源库目录列表
     *
     * @param mineResCatalog 我的资源库目录信息
     * @return 我的资源库目录集合
     */
    @Override
    public List<MineResCatalog> selectMineResCatalogList(MineResCatalog mineResCatalog) {
        return mineResCatalogMapper.selectMineResCatalogList(mineResCatalog);
    }

    /**
     * 新增我的资源库目录
     *
     * @param mineResCatalog 我的资源库目录信息
     * @return 结果
     */
    @Override
    public int insertMineResCatalog(MineResCatalog mineResCatalog) {
        return mineResCatalogMapper.insertMineResCatalog(mineResCatalog);
    }

    /**
     * 修改我的资源库目录
     *
     * @param mineResCatalog 我的资源库目录信息
     * @return 结果
     */
    @Override
    public int updateMineResCatalog(MineResCatalog mineResCatalog) {
        return mineResCatalogMapper.updateMineResCatalog(mineResCatalog);
    }

    /**
     * 删除我的资源库目录对象
     *
     * @param ids 需要删除的数据ID --目前单删除
     * @return 结果
     */
    @Override
    public int deleteMineResCatalogByIds(String ids) {
        //批量删除或者单删除
        List<String> idList = Arrays.asList(Convert.toStrArray(ids));
        int i = 0;
        for (String id : idList) {
            //判断目录下是否还有目录,以id为pid查询是否有子节点
            List<MineResCatalogVO> mineResCatalogVO = mineResCatalogMapper.queryChildMenuByPid(id);
            if (mineResCatalogVO != null && mineResCatalogVO.size() > 0) return -1;
            //判断当前节点下是否有资源
            List<MineResTable> mineResTableList = mineResTableMapper.queryResListByCatalogId(Integer.valueOf(id), null);
            if (mineResTableList != null && mineResTableList.size() > 0) return -2;
            i += mineResCatalogMapper.deleteMineResCatalogByIds(Convert.toStrArray(id));
        }
        return i;
    }

    /**
     * 根据老师id和资源类型查询我的资源库左侧目录菜单
     *
     * @param teaId
     * @param type
     * @return
     */
    @Override
    public List<MineResCatalogVO> queryLeftMenuByTeaIdAndType(Integer teaId, Integer type) {
        List<MineResCatalogVO> mineResCatalogVOList = mineResCatalogMapper.queryLeftMenuByTeaIdAndType(teaId, type);

        List<MineResCatalogVO> treeEX = getTreeEX(mineResCatalogVOList);
        return treeEX;
    }

    /**
     * 备课中心-资源库上传-我的资源库资源(自己资源和购买资源)
     * 1-4、2-5、3-6
     *
     * @param teaId
     * @param catalogTypeList
     * @return
     */
    @Override
    public List<MineResCatalog> selectMineResCatalogListByCreateIdAndCatalogTypeList(Integer teaId, List<Integer> catalogTypeList) {
        return mineResCatalogMapper.selectMineResCatalogListByCreateIdAndCatalogTypeList(teaId, catalogTypeList);
    }

    /**
     * 默认分组catalog_id的查找 老师id和默认分组类型catalogType
     * @param teaId
     * @param catalogType
     * @return
     */
    @Override
    public MineResCatalog defaultGroupQueryAndReturn(Integer teaId, Integer catalogType) {
        return mineResCatalogMapper.defaultGroupQueryAndReturn(teaId,catalogType);
    }

    public static List<MineResCatalogVO> getTreeEX(List<MineResCatalogVO> record) {
        return record.stream().filter(mineResCatalogVO -> {
            return 0 == (mineResCatalogVO.getPid());
        }).map(mineResCatalogVO -> {

            List<MineResCatalogVO> childEX = getChildEX(mineResCatalogVO.getCatalogId(), record, 1);

            mineResCatalogVO.setMenuChildren(childEX);
            return mineResCatalogVO;
        }).collect(Collectors.toList());
    }

    private static List<MineResCatalogVO> getChildEX(Integer catalogId, List<MineResCatalogVO> record, Integer depth) {
        return record.stream().filter(mineResCatalogVO -> {
            return catalogId.equals(mineResCatalogVO.getPid());
        }).map(mineResCatalogVO -> {
            mineResCatalogVO.setDepth(depth);

            List<MineResCatalogVO> childEX = getChildEX(mineResCatalogVO.getCatalogId(), record, depth + 1);

            mineResCatalogVO.setMenuChildren(childEX);
            return mineResCatalogVO;
        }).collect(Collectors.toList());
    }

}